/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.widget.user;

import java.util.List;

import sw6.digipecs.R;
import sw6.digipecs.model.ExtImageInSentenceBoard;
import sw6.digipecs.widget.DragableExtImageView;
import sw6.digipecs.widget.ExtImageView;
import sw6.digipecs.widget.dragdrop.DragLayout;
import android.content.Context;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;

public class SentenceBoardExtImageAdapter extends BaseAdapter {
	
	// TODO This is kinda copy/paste of the other ExtImage-adapters :(
	// TODO Could be embedded in Sentence board layout..

	private Context mContext;
	private List<ExtImageInSentenceBoard> mImageList;
	private LayoutInflater mLayoutInflater;
	private DragLayout mDragLayout;

	public SentenceBoardExtImageAdapter(Context context, List<ExtImageInSentenceBoard> images, DragLayout dl) {
		mContext = context;
		mImageList = images;
		mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mDragLayout = dl;
	}

	@Override
	public int getCount() {
		return mImageList.size();
	}

	@Override
	public Object getItem(int pos) {
		return mImageList.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		FrameLayout view;
		ExtImageView imageView;
		
		ExtImageInSentenceBoard image = mImageList.get(pos);
		
		// Create view
		if (image.isLocked()) {
			imageView = new ExtImageView(mContext);
			imageView.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);							 
					vibrator.vibrate(150);
					return false;
				}
			});
		} else {
			final DragableExtImageView deiv = new DragableExtImageView(mContext, mDragLayout, null, null);
			imageView = deiv;
		}
		
		// Convert view
		if (convertView == null) {
            view = (FrameLayout) mLayoutInflater.inflate(R.layout.placeholder, parent, false);
        } else {
        	view = (FrameLayout) convertView;
        	view.removeAllViews();
        }
		
		// Set image
		imageView.setImage(image);
		view.addView(imageView);
		
		return view;
	}

}
